package com.example.exampleproject.datamanager;

import com.example.exampleproject.base.BaseDataManager;
import com.example.exampleproject.base.BaseObserver;
import com.example.exampleproject.entity.BaseResponse;
import com.example.exampleproject.entity.RequestBody;
import com.example.exampleproject.entity.Weather;
import com.example.exampleproject.net.ApiDefinition;
import com.example.exampleproject.net.ApiService;

import javax.inject.Inject;
import javax.inject.Singleton;

import io.reactivex.Observable;
import io.reactivex.disposables.Disposable;
import io.reactivex.subjects.PublishSubject;


@Singleton
public class MainDataManager extends BaseDataManager {

    private PublishSubject<BaseResponse<Weather>> userPS = PublishSubject.create();

    ApiDefinition mApiService;

    @Inject
    public MainDataManager(ApiService apiService) {
        mApiService = apiService.getService();
    }

    public void requestWeather(RequestBody request) {
        Observable<BaseResponse<Weather>> observable = Observable.concat(
                getNetCacheObservable("weather"),
                mApiService.getWeather(request.getLat())
        );
        publish(observable, userPS);
    }

    public Disposable subscribeToGetUser(BaseObserver<BaseResponse<Weather>> consumer) {
        return subscribe(userPS, consumer);
    }

}
